package com.sunyard.manage.controller;

import com.sunyard.constant.ErrMessageConst;
import com.sunyard.dal.bo.device.key.KeyAddBO;
import com.sunyard.dal.bo.device.key.KeyCheckBO;
import com.sunyard.dal.bo.device.key.KeyCheckDetailBO;
import com.sunyard.dal.bo.device.key.KeyCopyQueryBO;
import com.sunyard.dal.bo.device.key.KeyDeleteBO;
import com.sunyard.dal.bo.device.key.KeyQueryBO;
import com.sunyard.dal.bo.device.key.KeyUpdateBO;
import com.sunyard.dal.bo.device.key.KeyUpdateStateBO;
import com.sunyard.dal.bo.device.key.KeyUploadLogBO;
import com.sunyard.dal.bo.PaginationBO;
import com.sunyard.dal.dto.KeyCheckDetailResDTO;
import com.sunyard.dal.dto.KeyCopyQueryResDTO;
import com.sunyard.dal.dto.KeyQueryResDTO;
import com.sunyard.manage.service.KeyService;
import com.sunyard.utils.Response.BaseResult;
import com.sunyard.utils.Response.GetResult;
import com.sunyard.utils.manage.DataGrid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(tags = "KeyController", description = "终端管理")
@Slf4j
@RestController
@RequestMapping(value = "/key", method = RequestMethod.POST)
public class KeyController {

    @Autowired
    KeyService keyService;

    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("分页查询终端信息")
    public BaseResult<DataGrid<KeyQueryResDTO>> getKeyByPage(@RequestBody PaginationBO<KeyQueryBO> queryBO, HttpServletRequest request) {
        try {
            return keyService.getKeyByPage(queryBO, request);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    @RequestMapping(value = "/getKeyCopyByPage", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("分页查询终端审核信息")
    public BaseResult<DataGrid<KeyCopyQueryResDTO>> getKeyCopyByPage(@RequestBody PaginationBO<KeyCopyQueryBO> queryBO, HttpServletRequest request) {
        try {
            return keyService.getKeyCopyByPage(queryBO, request);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    @RequestMapping(value = "/addKey", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("新增终端信息")
    public BaseResult addKey(@Validated @RequestBody KeyAddBO addBO, HttpServletRequest request) {
        try {
            return keyService.addKey(addBO, request);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    @RequestMapping(value = "/deleteKey", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("删除终端信息")
    public BaseResult deleteKey(@Validated @RequestBody KeyDeleteBO deleteBO) {
        try {
            return keyService.deleteKey(deleteBO);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    @RequestMapping(value = "/updateKey", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("修改终端信息")
    public BaseResult updateKey(@Validated @RequestBody KeyUpdateBO updateBO) {
        try {
            return keyService.updateKey(updateBO);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    @RequestMapping(value = "/updateState", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("终端启用和停用")
    public BaseResult updateState(@Validated @RequestBody KeyUpdateStateBO updateStateBO) {
        try {
            return keyService.updateState(updateStateBO);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    @RequestMapping(value = "/exportKey", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("终端信息导出")
    public BaseResult exportKey(HttpServletRequest request, HttpServletResponse response) {
        try {
            return keyService.exportKey(request, response);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    @RequestMapping(value = "/uploadLog", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("上传操作日志")
    public BaseResult uploadLog(@Validated @RequestBody KeyUploadLogBO uploadLogBO, HttpServletRequest request) {
        try {
            return keyService.uploadLog(uploadLogBO, request);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    @RequestMapping(value = "/checkKey", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("终端审核")
    public BaseResult checkKey(@Validated @RequestBody KeyCheckBO checkBO, HttpServletRequest request) {
        try {
            return keyService.checkKey(checkBO, request);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    @RequestMapping(value = "/checkDetail", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("终端审核详情")
    public BaseResult<KeyCheckDetailResDTO> checkDetail(@Validated @RequestBody KeyCheckDetailBO checkBO, HttpServletRequest request) {
        try {
            return keyService.checkDetail(checkBO, request);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }
}
